package com.outsourcing.bottle.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.BottleFeedListEntry;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.ImageLoader;

public class BottleDetailFeedAdapter extends BaseAdapter {
	protected static final String TAG = "PublicActivityAdapter";

	private Context context;

	private List<BottleFeedListEntry> msgs;
	public ImageLoader imageLoader;


	public BottleDetailFeedAdapter(Context context, List<BottleFeedListEntry> msgs) {
		super();
		this.context = context;
		this.msgs = msgs;
		imageLoader = new ImageLoader(context, AppContext.BottleTimelineIcon);
	}

	@Override
	public int getCount() {
		return (msgs!=null&&msgs.size()>0)?msgs.size():0;
	}

	@Override
	public Object getItem(int position) {
		return (msgs!=null&&msgs.size()>0)?msgs.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	BottleDetailViewHolder holder;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			holder = new BottleDetailViewHolder();

			// Item layout
			convertView = LayoutInflater.from(context).inflate(
					R.layout.bottle_comment_list_detail_item, null);

			holder.mAuthorView = (RemoteImageView) convertView.findViewById(R.id.comment_profile_photo);
			holder.mLink = (ImageView)convertView.findViewById(R.id.iv_feed_comment_white);
			holder.mfeedContent = (TextView) convertView.findViewById(R.id.tv_comment_feed_content);
			holder.mfeedReplyComment = (TextView)convertView.findViewById(R.id.tv_comment_feed_reply);
			holder.mfeedReplyPhoto = (ImageView)convertView.findViewById(R.id.photo);
			
			convertView.setTag(holder);

		} else {
			holder = (BottleDetailViewHolder) convertView.getTag();
		}

		final BottleFeedListEntry message = (BottleFeedListEntry) getItem(position);
		holder.mAuthorView.setDefaultImage(R.drawable.avatar_default_small);
		holder.mAuthorView.setImageUrl(message.getFeed_avatar());
		holder.mfeedContent.setText(message.getFeed_nickname()+" "+message.getFeed_content()+" "+message.getFeed_time()+" @"+message.getFeed_location());
		
		if (null!=message.getFeed_replycomment()&&message.getFeed_replycomment().length()>0) {
			holder.mfeedReplyComment.setVisibility(View.VISIBLE);
			holder.mfeedReplyComment.setText(message.getFeed_replycomment());
		}else {
			holder.mfeedReplyComment.setVisibility(View.GONE);
		}
		if (null!=message.getFeed_commentpic()&&message.getFeed_commentpic().length()>0) {
			holder.mfeedReplyPhoto.setVisibility(View.VISIBLE);
			imageLoader.DisplayImage(message.getFeed_commentpic(), holder.mfeedReplyPhoto,
					R.drawable.avatar_default_small);
		}else {
			holder.mfeedReplyPhoto.setVisibility(View.GONE);
		}
		
		return convertView;
	}

	public class BottleDetailViewHolder {
		public RemoteImageView mAuthorView;// 用户头像
		public ImageView mLink;//白色的小锁图片 不知道干嘛用的
		public TextView mfeedContent;// 回复内容
		public LinearLayout mfeedReplyCommentLayout;
		public TextView mfeedReplyComment;//
		public ImageView mfeedReplyPhoto;
	}
}
